package com.lagou.springbootwork.controller;

import com.github.pagehelper.PageInfo;
import com.lagou.springbootwork.entity.Article;
import com.lagou.springbootwork.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName ArticleController
 * @Deseription TODO
 * @Author fx
 * @Date 2021/1/7 0007 22:36
 * @Version 1.0
 **/
@Controller
public class ArticleController {

    @Autowired
    private IArticleService articleService;

    /**
     * @Author fx
     * @Description 分页获取所有文章信息
     * @ClassName ArticleController
     * @Date 15:52 2021/1/9 0009
     * @Param
     * @return
     **/
    @RequestMapping("getArticleListByPage")
    public String getArticleListByPage(HttpServletRequest request, Model model) throws Exception{
        String pageStr = request.getParameter("page");
        String sizeStr = request.getParameter("size");
        //当前页
        int page = pageStr == null ? 1 : Integer.parseInt(pageStr);
        //页面显示条数
        int size = sizeStr == null ? 3 : Integer.parseInt(sizeStr);
        //查询文章数据
        PageInfo pageInfo = articleService.getAllByPage(page, size);

        page = page < 0 ? 1 : page;
        page = page > pageInfo.getPageSize() ? pageInfo.getPageSize() : page;

        model.addAttribute("articleList",pageInfo.getList());
        model.addAttribute("page",page);
        model.addAttribute("pageSize",pageInfo.getPageSize());
        return "/client/index";
    }
}
